home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / PRGMANIA / VERSION1.5 / GEMUTIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-15  |  16.7 KB  |  608 lines

  1. /*************************************************************************
  2.  * Module contenant les fonctions utilitaires des autres modules         *
  3.  *************************************************************************/
  4. #include "WindGem.h"
  5. #include "winproto.h"
  6. #include "user.h"
  7.  
  8. /*************************************************************************
  9.  * Duplication d'un arbre d'objets.                                      *
  10.  *                                                                       *
  11.  * Reprise d'EGEM220.                                                    *
  12.  *************************************************************************/
  13. OBJECT *copy_tree(OBJECT *tree)
  14. {
  15.     OBJECT *new;
  16.     TEDINFO *nted;
  17.     register OBJECT *obj=tree;
  18.     register TEDINFO *ted;
  19.     register char *d,*s;
  20.     register long size=0,strsize=0;
  21.     register    int i;
  22.  
  23.     for (;;)
  24.     {
  25.         size += sizeof(OBJECT);
  26.         switch ((unsigned char) obj->ob_type)
  27.         {
  28.         case G_TEXT:
  29.         case G_FTEXT:
  30.         case G_BOXTEXT:
  31.         case G_FBOXTEXT:
  32.             ted = obj->ob_spec.tedinfo;
  33.             strsize += sizeof(TEDINFO);
  34.             strsize += ted->te_txtlen*2+ted->te_tmplen+3+1;
  35.             strsize &= ~1;
  36.             break;
  37.         case G_STRING:
  38.         case G_TITLE:
  39.         case G_BUTTON:
  40.             strsize += strlen(obj->ob_spec.free_string)+1+1;
  41.             strsize &= ~1;
  42.             break;
  43.         }
  44.         if (obj->ob_flags & LASTOB)
  45.             break;
  46.         obj++;
  47.     }
  48.  
  49.     if ((new=(OBJECT *) calloc(1,size+strsize))!=NULL)
  50.         for (memcpy(obj=new,tree,size),nted=(TEDINFO *) (((char *) new)+size);;)
  51.         {
  52.             switch ((unsigned char) obj->ob_type)
  53.             {
  54.             case G_TEXT:
  55.             case G_FTEXT:
  56.             case G_BOXTEXT:
  57.             case G_FBOXTEXT:
  58.                 ted = obj->ob_spec.tedinfo;
  59.                 obj->ob_spec.tedinfo = nted;
  60.                 *nted = *ted;
  61.                 nted->te_ptext = d = (char *) &nted[1];
  62.                 for (s=ted->te_ptext,i=ted->te_txtlen;--i>=0;*d++=*s++);
  63.                 nted->te_ptmplt = d;
  64.                 for (s=ted->te_ptmplt,i=ted->te_tmplen;--i>=0;*d++=*s++);
  65.                 nted->te_pvalid = d;
  66.                 for (s=ted->te_pvalid,i=ted->te_txtlen;--i>=0;*d++=*s++);
  67.                 nted = (TEDINFO *) ((long) (d+1) & ~1);
  68.                 break;
  69.             case G_STRING:
  70.             case G_TITLE:
  71.             case G_BUTTON:
  72.                 obj->ob_spec.free_string = d = strcpy((char *) nted,obj->ob_spec.free_string);
  73.                 nted = (TEDINFO *) ((long) (d+strlen(d)+1+1) & ~1);
  74.             }
  75.             if (obj->ob_flags & LASTOB)
  76.                 break;
  77.             obj++;
  78.         }
  79.     return (new);
  80. }
  81.  
  82. /*-----------------------------------------------------------------------*
  83.  * Affichage d'un icone dans une fenetre iconifiee                       *
  84.  *-----------------------------------------------------------------------*/
  85. void DrawIcone (int handle, GRECT *zone)
  86. {
  87.     int xw, yw, ww, hw, pxy[4];
  88.  
  89.     /* Coordonnées zone de travail : */
  90.     wind_get (handle, WF_WORKXYWH, &xw, &yw, &ww, &hw);
  91.     pxy[0] = xw;                                        /* Préparer effacement fenêtre */
  92.     pxy[1] = yw;
  93.     pxy[2] = xw + ww - 1;
  94.     pxy[3] = yw + hw - 1;
  95.     vswr_mode (Sys->VdiHandle, MD_REPLACE); /* Dessin en mode Remplacement */
  96.     vsf_color (Sys->VdiHandle, 0);          /* Couleur blanche */
  97.     v_bar (Sys->VdiHandle, pxy);                   /* "Vider" la fenêtre */
  98.  
  99.     (Sys->AdrIcone)->ob_x = xw;
  100.     (Sys->AdrIcone)->ob_y = yw;
  101.  
  102.     objc_draw (Sys->AdrIcone, ICONE, 1, xw, yw, ww, hw);
  103. }
  104.  
  105. /*-----------------------------------------------------------------------*
  106.  * Capture d'un fond.                                                    *
  107.  *-----------------------------------------------------------------------*/
  108. void get_bkgr (int of_x, int of_y, int of_w, int of_h, MFDB *img)
  109. {
  110.     int pxy[8];
  111.     unsigned long taille;
  112.     MFDB ecr={0};
  113.  
  114.     of_x -= 3;                                        /* Prévoir une marge de sécurité autour  */
  115.     of_y -= 3;                                        /* de la zone, pour le cas où on aurait  */
  116.     of_w += 5;                                        /* besoin de sauvegarder le fond d'un    */
  117.     of_h += 5;                                        /* formulaire avec un attribut OUTLINED. */
  118.  
  119.     /* Taille tampon de copie fond */
  120.     taille = ((((unsigned long)(of_w / 16L) + 1L) * 2L * (size_t)Sys->Nplane) * (size_t)of_h) + 256L;
  121.     img->fd_addr = (FDADDR)lalloc (taille);                            /* Réserver tampon */
  122.     img->fd_w = of_w;                              /* Remplir la structure MFDB */
  123.     img->fd_h = of_h;
  124.     img->fd_wdwidth = (of_w / 16) + ((of_w % 16) != 0);
  125.     img->fd_stand = 1;
  126.     img->fd_nplanes = Sys->Nplane;
  127.  
  128.     pxy[0] = of_x;                                                                     /* Remplir la tableau */
  129.     pxy[1] = of_y;
  130.     pxy[2] = pxy[0] + of_w - 1;
  131.     pxy[3] = pxy[1] + of_h - 1;
  132.     pxy[4] = 0;
  133.     pxy[5] = 0;
  134.     pxy[6] = of_w - 1;
  135.     pxy[7] = of_h - 1;
  136.     v_hide_c (Sys->VdiHandle);                                                    /* Virer la souris */
  137.     vro_cpyfm (Sys->VdiHandle, S_ONLY, pxy, &ecr, img);    /* Copier l'image  */
  138.     v_show_c (Sys->VdiHandle, TRUE);                                        /* Remet la souris */
  139. }
  140.  
  141. /*-----------------------------------------------------------------------*
  142.  * Reaffichage d'un fond.                                                *
  143.  *-----------------------------------------------------------------------*/
  144. void put_bkgr (int of_x, int of_y, int of_w, int of_h, MFDB *img)
  145. {
  146.     int pxy[8];
  147.     MFDB ecr={0};
  148.  
  149.     of_x -= 3;                                        /* Prévoir une marge de sécurité autour  */
  150.     of_y -= 3;                                        /* de la zone, pour le cas où on aurait  */
  151.     of_w += 5;                                        /* besoin de sauvegarder le fond d'un    */
  152.     of_h += 5;                                        /* formulaire avec un attribut OUTLINED. */
  153.  
  154.     pxy[0] = 0;                                        /* Remplir le tableau                    */
  155.     pxy[1] = 0;
  156.     pxy[2] = of_w - 1;
  157.     pxy[3] = of_h - 1;
  158.     pxy[4] = of_x;
  159.     pxy[5] = of_y;
  160.     pxy[6] = pxy[4] + pxy[2] - 1;
  161.     pxy[7] = pxy[5] + pxy[3] - 1;
  162.     v_hide_c (Sys->VdiHandle);                              /* Cache la souris */
  163.     vro_cpyfm (Sys->VdiHandle, S_ONLY, pxy, img, &ecr);    /* Copier l'image  */
  164.     v_show_c (Sys->VdiHandle, TRUE);                      /* Remet la souris */
  165.     free (img->fd_addr);                             /* Libére la mémoire */
  166. }
  167.  
  168. /*-----------------------------------------------------------------------*
  169.  * Recherche de l'objet pere d'un objet                                  *
  170.  *-----------------------------------------------------------------------*/
  171. int parent (OBJECT *adr, int object)
  172. {
  173.     register int i;
  174.  
  175.   i = object;                                                                         /* Partir de cet objet */
  176.   do
  177.   {
  178.     i = adr[i].ob_next;                                                     /* Passer au suivant... */
  179.   } while (i > object);                      /* Jusqu'a revenir au pere. */
  180.  
  181.   return i;                                         /* Retourner le pere */
  182. }
  183.  
  184. /*-----------------------------------------------------------------------*
  185.  * Recuperer numero d'item menu                                          *
  186.  *-----------------------------------------------------------------------*/
  187. int m_title (OBJECT *adresse, int option)
  188. {
  189.     register menu = 1, k = 2;
  190.     int pere, titre;
  191.  
  192.   pere = parent (adresse, option);
  193.   while (adresse[(k++) + 1].ob_type != G_BOX) ;         /* Chercher 1° G_BOX */
  194.  
  195.   while (k != pere)
  196.   {
  197.     k = adresse[k].ob_next;                         /* Chercher menu correspondant */
  198.     menu++;                                         /* Les compter                 */
  199.   }
  200.  
  201.   k = 3;
  202.   do
  203.   {
  204.     titre = k++;                                            /* L'affecter                  */
  205.   } while ((k - 3) != menu);
  206.  
  207.   return titre;
  208. }
  209.  
  210. /*-----------------------------------------------------------------------*
  211.  * Fonction de mise en place d'un Clipping.                              *
  212.  *-----------------------------------------------------------------------*/
  213. void set_clip (int clip_flag, GRECT *area)
  214. { /* Active ou désactive le clipping d'une zone */
  215.     int pxy[4];
  216.  
  217.     pxy[0] = area->g_x;
  218.     pxy[1] = area->g_y;
  219.     pxy[2] = area->g_w + area->g_x - 1;
  220.     pxy[3] = area->g_h + area->g_y - 1;
  221.     vs_clip (Sys->VdiHandle, clip_flag, pxy);
  222. }
  223.  
  224. /*-----------------------------------------------------------------------*
  225.  * Simulation d'un message.                                              *
  226.  *-----------------------------------------------------------------------*/
  227. void EnvoiMessage(int type, int handle, int message)
  228. {
  229.     buff[0] = type;
  230.     buff[1] = Sys->AppId;
  231.     buff[2] = 0;
  232.     buff[3] = handle;
  233.     buff[4] = message;
  234.     buff[5] = 0;
  235.     buff[6] = 0;
  236.     buff[7] = 0;
  237.  
  238.      appl_write (Sys->AppId, 16, buff);                       /* Envoi du message */
  239. }
  240.  
  241. void EnvoiRedraw(int handle, int x, int y, int w, int h)
  242. {
  243.     buff[0] = WM_REDRAW;
  244.     buff[1] = Sys->AppId;
  245.     buff[2] = 0;
  246.     buff[3] = handle;
  247.     buff[4] = x;
  248.     buff[5] = y;
  249.     buff[6] = w;
  250.     buff[7] = h;
  251.  
  252.      appl_write (Sys->AppId, 16, buff);                       /* Envoi du message */
  253. }
  254. /*-----------------------------------------------------------------------*
  255.  * Gestion de la frappe d'une touche au clavier                          *
  256.  *-----------------------------------------------------------------------*/
  257. int stdkey (unsigned char *k)
  258. {
  259.     KEYTAB *kt;
  260.  
  261.     kt = Keytbl((void *)-1, (void *)-1, (void *)-1);
  262.     *k = toupper (kt->shift[(char)((Sys->key) >> 8)]);
  263.  
  264.     return toupper (kt->shift[(char)((Sys->key) >> 8)]);
  265. }
  266.  
  267. /*-----------------------------------------------------------------------*
  268.  * Suppression des espaces en debut et fin de chaine <str>               *
  269.  *-----------------------------------------------------------------------*/
  270. char *trim (char *str)
  271. {
  272.     register char *s;
  273.     register int i = 0;
  274.     char chaine[MAX_LEN];
  275.  
  276.   while (*(str + i) == ' ')
  277.     i++;
  278.   strcpy (chaine, (str + i));
  279.  
  280.   s = chaine + strlen (chaine) - 1;
  281.   for( ; (*s == ' ') && (s >= chaine) ; *s-- = 0);
  282.   strcpy (str, chaine);
  283.  
  284.   return str;
  285. }
  286.  
  287. /*-----------------------------------------------------------------------*/
  288. char *strnpcpy(char *dest, char *start, char *stop)
  289. /*-----------------------------------------------------------------------*
  290.  * Fonction de bibliotheque : Copie dans "dest" la portion de chaine a   *
  291.  * partir du pointeur "start", jusqu'au caractere place juste avant le   *
  292.  * pointeur "stop". "start" et "stop" doivent obligatoirement pointer    *
  293.  * dans la meme chaine. Dans tous les cas "dest" est terminee par le     *
  294.  * caractere de fin de chaine '\0'.                                      *
  295.  *-----------------------------------------------------------------------*/
  296. {
  297.     char *ptr_dest = dest;
  298.  
  299.     while (start < stop && (*(ptr_dest)++ = *(start++)) != 0);
  300.     *ptr_dest = '\0';
  301.  
  302.     return dest;
  303. }
  304.  
  305. char *get_text (OBJECT *adr, int object)
  306. {
  307.     int type;
  308.     char *retour = "";
  309.  
  310.     type = adr[object].ob_type & 0xFF;    /* Lire le type de l'objet  */
  311.                                                                             /* en virant le type etendu */
  312.  
  313.     /* Si ob_spec désigne le texte */
  314.     if ((type == G_STRING) || (type == G_BUTTON))
  315.         retour = (adr[object].ob_spec.free_string);
  316.     /* Si ob_spec désigne une structure TEDINFO */
  317.     else if ((type == G_TEXT) || (type == G_BOXTEXT) || (type == G_FTEXT) || (type == G_FBOXTEXT))
  318.         retour = (adr[object].ob_spec.tedinfo->te_ptext);
  319.     else if (type == G_USERDEF)
  320.         retour = ((char *)(adr[object].ob_spec.userblk->ub_parm));
  321.  
  322.     return retour;    /* Retourner pointeur sur la chaîne */
  323. }
  324.  
  325. void set_text (OBJECT *adr, int object, char *string)
  326. {
  327.     int type;
  328.  
  329.     type = adr[object].ob_type & 0xFF;    /* Lire le type de l'objet  */
  330.                                                                             /* en virant le type etendu */
  331.  
  332.     /* Si ob_spec désigne le texte */
  333.     if ((type == G_STRING) || (type == G_BUTTON))
  334.         strcpy (adr[object].ob_spec.free_string, string);
  335.     /* Si ob_spec désigne une structure TEDINFO */
  336.     else if ((type == G_TEXT) || (type == G_BOXTEXT) || (type == G_FTEXT) || (type == G_FBOXTEXT))
  337.         strcpy (adr[object].ob_spec.tedinfo->te_ptext, string);
  338.     else if (type == G_USERDEF)
  339.         strcpy ((char *)(adr[object].ob_spec.userblk->ub_parm), string);
  340. }
  341.  
  342. long exist (char *name, int att)
  343. {
  344. int re;
  345.  
  346.     re = Fsfirst (name, att);
  347.     if ((re == -34) || (re == -33))        /* Si rien n'est trouvé */
  348.         return (FALSE);                                    /* retourner FALSE */
  349.     else                                                            /* Sinon */
  350.     {
  351.         if ((att & FA_SUBDIR) != FA_SUBDIR)    /* Si ce n'est pas un dossier */
  352.             return dtabuffer.d_length;                /* Retourner la taille */
  353.         else                                                                /* Si c'est un dossier */
  354.             return (TRUE);                                        /* Retourner TRUE */
  355.     }
  356. }
  357.  
  358. /* path () Cherche et retourne le chemin de l'application :            */
  359. char *path (char *chemin)
  360. {
  361. int drive;
  362.  
  363.     drive = Dgetdrv ();                            /* Lecteur courant */
  364.     chemin[0] = (char) (65 + drive);
  365.     chemin[1] = ':';
  366.     Dgetpath (&chemin[2], drive + 1);    /* Chemin lecteur courant */
  367.     strcat (chemin, "\\");
  368.     return chemin;
  369. }
  370.  
  371. /* selector () Sélecteur :                                                                             */
  372. int selector (char *chemin, char *ext, char *file, char *title)
  373. {
  374. char ch[120], fi[13];
  375. int retour, i;
  376.  
  377.     strcpy (ch, chemin);
  378.     strcat (ch, ext);                         /* Chemin et sélection */
  379.     strcpy (fi, file);
  380.     if ((Sversion () >> 8) <= 20)
  381.         fsel_input (ch, fi, &retour);                     /* Appel du sélecteur */
  382.     else
  383.         fsel_exinput (ch, fi, &retour, title);    /* Appel du sélecteur */
  384.     if (retour)                                        /* Si pas ANNULER... */
  385.     {
  386.         for (i = (int)strlen (ch) ; ch[i] != '\\' ; ch[i--] = '\0') ;
  387.         strcpy (chemin, ch);
  388.         strcpy (file, fi);
  389.     }
  390.     return retour;
  391. }
  392.  
  393. /* extension () Ajuste extension :                                                                */
  394. void extension (char *filename, char *ext)
  395. {                             /* L'EXTENSION DOIT ETRE TRANSMISE AVEC LE POINT */
  396. int t;
  397.  
  398.     t = (int)strlen (filename);                                /* Longueur du nom de fichier */
  399.     while (filename[t] != '.' && t > ZERO)     /* Chercher le '.' à partir de la droite */
  400.         t--;
  401.     if (t > ZERO)                                        /* Si trouvé, */
  402.     {
  403.         memcpy (filename + t, ext, 4);             /* ajouter l'extention à partir de là */
  404.         filename[t + 4] = '\0';
  405.     }
  406.     else                                                        /* Sinon, */
  407.         strcat (filename, ext);                            /* l'ajouter à la fin */
  408. }
  409.  
  410. /* litdate () Lit la date système ou fichier et conv. chaîne :        */
  411. void litdate (char *dat, unsigned int fdate)
  412. {
  413. int d, a, m, j;
  414. char ac[3], mc[3], jc[3];
  415.  
  416.     if (fdate == ZERO)
  417.         d = Tgetdate ();
  418.     else
  419.         d = fdate;
  420.     j = d & 0x1f;
  421.     m = (d >> 5) & 0x0f;
  422.     a = ((d >> 9) & 0x7f) + 80;
  423.  
  424.     itoa (j, jc, 10);
  425.     if (strlen (jc) == 1)
  426.     {
  427.         jc[1] = jc[0];
  428.         jc[0] = '0';
  429.         jc[2] = '\0';
  430.     }
  431.     itoa (m, mc, 10);
  432.     if (strlen (mc) == 1)
  433.     {
  434.         mc[1] = mc[0];
  435.         mc[0] = '0';
  436.         mc[2] = '\0';
  437.     }
  438.     itoa (a, ac, 10);
  439.     if (strlen (ac) == 1)
  440.     {
  441.         ac[1] = ac[0];
  442.         ac[0] = '0';
  443.         ac[2] = '\0';
  444.     }
  445.  
  446.     strcpy (dat, jc);
  447.     strcat (dat, mc);
  448.     strcat (dat, ac);
  449. }
  450.  
  451. /* convdate () Convertir date chaîne en valeur :                                    */
  452. void convdate (char *date, unsigned int *fdate)
  453. {
  454. int j, m, a, new_date = ZERO;
  455. char jc[3], mc[3], ac[3];
  456.  
  457.     jc[0] = date[0];
  458.     jc[1] = date[1];
  459.     jc[2] = '\0';
  460.  
  461.     mc[0] = date[2];
  462.     mc[1] = date[3];
  463.     mc[2] = '\0';
  464.  
  465.     ac[0] = date[4];
  466.     ac[1] = date[5];
  467.     ac[2] = '\0';
  468.  
  469.     j = atoi (jc);
  470.     m = atoi (mc);
  471.     a = atoi (ac) - 80;
  472.  
  473.     new_date |= j;
  474.     new_date |= (m << 5);
  475.     new_date |= (a << 9);
  476.  
  477.     *fdate = new_date;
  478. }
  479.  
  480. /*
  481. Ajoute <ch> avant <chaine>
  482. */
  483. char *strinsert (char *chaine, char *ch)
  484. {
  485.     char temp[256];
  486.     
  487.     strcpy(temp, chaine);
  488.     strcpy(chaine, ch);
  489.     strcat(chaine, temp);
  490.     
  491.     return chaine;
  492. }
  493.  
  494. /*
  495. Complete une chaine de caractere par des <car> avant ou apres
  496. suivant <sens> (1 ou -1)
  497. */
  498. char *strcomplete (char *chaine, int lng, char car, int sens)
  499. {
  500.     int i, j = 0;
  501.     char ch[256];
  502.     
  503.     i = lng - strlen(chaine);
  504.     
  505.     if (i > 0)
  506.     {
  507.         ch[i] = '\0';
  508.         while (i > 0)
  509.             ch[--i] = car;
  510.     
  511.         switch (sens)
  512.         {
  513.             case INS_AVANT :
  514.                 strinsert(chaine, ch);
  515.                 break;
  516.             case INS_APRES :
  517.                 strcat(chaine, ch);
  518.         }
  519.     }
  520.  
  521.     return chaine;    
  522. }
  523.  
  524. /*
  525. Copy dans <res> de la sous-chaine commencant en position <deb>
  526. et finissant par <fin> compris de la chaine <chaine>
  527. */
  528. char *strcopy(char *res, char *chaine, int deb, int fin)
  529. {
  530.     int i = 0, l = strlen(chaine);
  531.  
  532.     if (deb < 0 || fin < 0 || fin < deb || deb > l || fin > l)
  533.     {
  534.         strcpy(res,"");
  535.         return (char *)0;
  536.     }
  537.  
  538.     while (deb <= fin && chaine[deb])
  539.         res[i++] = chaine[deb++];
  540.  
  541.     res[i] = '\0';
  542.     
  543.     return res;    
  544. }
  545.  
  546. char *strleft (char *res, char *chaine, int nb)
  547. {
  548.     return strcopy (res, chaine, 0, nb);
  549. }
  550.  
  551. char *strright (char *res, char *chaine, int nb)
  552. {
  553.     register int taille = strlen(chaine);
  554.   return strcopy (res, chaine, taille - nb, taille);
  555. }    
  556.  
  557. char *strmid (char *res, char *chaine, int deb, int nb)
  558. {
  559.     return strcopy (res, chaine, deb, deb + nb);
  560. }
  561.  
  562. /*-----------------------------------------------------------------------*
  563.  * Fonction de remplissage d'un tableau.                                 *
  564.  *-----------------------------------------------------------------------*/
  565. void fill_tab (int *pxy, int n, ...)
  566. {
  567.     int i, val;
  568.     va_list pa;
  569.  
  570.     va_start (pa, n);
  571.     for (i = ZERO ; i < n ; i++)
  572.     {
  573.         val = va_arg (pa, int);
  574.         *(pxy + i) = val;
  575.     }
  576.     va_end (pa);
  577. }
  578.  
  579. void cre_fichier(char *chemin, char *fic, char *fichier)
  580. {
  581.     char *pos;
  582.  
  583.     pos = strrchr(chemin,'\\');
  584.     strnpcpy(fichier, chemin, pos + 1);
  585.     strcat(fichier, fic);    
  586. }
  587.  
  588. void GetCharSize (int *Wchar, int *Hchar)
  589. {
  590.     *Wchar = Sys->Wchar;
  591.     *Hchar = Sys->Hchar;
  592. }
  593.  
  594. void GetDeskXYWH (int *Xdesk, int *Ydesk, int *Wdesk, int *Hdesk)
  595. {
  596.     *Xdesk = Sys->Xdesk;
  597.     *Ydesk = Sys->Ydesk;
  598.     *Wdesk = Sys->Wdesk;
  599.     *Hdesk = Sys->Hdesk;
  600. }
  601.  
  602. void GetVideoRes (int *Xres, int *Yres, int *Nplane)
  603. {
  604.     *Xres = Sys->Xres;
  605.     *Yres = Sys->Yres;
  606.     *Nplane = Sys->Nplane;
  607. }
  608.